<?php

namespace App\Http\Controllers\Api;

use App\Helpers\ResponseData;
use App\Http\Controllers\Controller;
use App\Models\BaseConfig;
use App\Models\Car;
use App\Models\CarService;
use Illuminate\Http\Request;
//精选服务
class ServiceController extends Controller
{
    use ResponseData;
    public function list(Request $request,CarService $service){
        $data = $service->get();
        return $this->success($data);
    }
    public function category(Request $request){
        $data = BaseConfig::select('car_category')->first();
        if($data->car_category){
            $source = explode(",",$data->car_category);
            $arr = array();
            foreach($source as $item){
                $price = $this->getMinPrice($item);
                if($price){
                    $map = [
                        'key'=>$item,
                        'value'=>$price
                    ];
                    array_push($arr,$map);
                }
            }
            return $this->success($arr);
        }else{
            return $this->failed('未获取到类型',201);
        }
    }
    protected function getMinPrice($catgory){
        return Car::query()->where('category',$catgory)->min('minimum_price');
    }
}
